package com.chenxin.wms.service.impl;

import com.chenxin.common.constant.WmsConstants;
import com.chenxin.common.core.domain.ProcResult;
import com.chenxin.common.enums.WmsTaskType;
import com.chenxin.wms.domain.TaskEntity;
import com.chenxin.wms.domain.po.TaskInfo;
import com.chenxin.wms.domain.vo.TaskVO;
import com.chenxin.wms.mapper.TaskMapper;
import com.chenxin.wms.process.task.IProcTaskCancelById;
import com.chenxin.wms.process.task.ITaskProcess;
import com.chenxin.wms.service.ITaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 任务查询Service业务层处理
 *
 * @author gry
 * @date 2025-04-10
 */
@Service
@Slf4j
public class TaskServiceImpl implements ITaskService {
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private IProcTaskCancelById taskCancelProcess;

    /**
     * 查询任务查询
     *
     * @param taskId 任务查询主键
     * @return 任务查询
     */
    @Override
    public TaskVO selectTaskByTaskId(Long taskId) {
        return taskMapper.selectTaskInfoById(taskId);
    }

    /**
     * 查询任务查询列表
     *
     * @param taskVo 任务查询
     * @return 任务查询
     */
    @Override
    public List<TaskVO> selectTaskList(TaskVO taskVo) {
        return taskMapper.selectTaskViewList(taskVo);
    }

    @Override
    public List<TaskVO> selectCheckTaskList(TaskVO taskVo) {
        taskVo.setTaskType(WmsTaskType.CHECK.getCode());
        return taskMapper.selectCheckTaskViewList(taskVo);
    }

    /**
     * 新增任务查询
     *
     * @param taskEntity 任务查询
     * @return 结果
     */
    @Override
    public int insertTask(TaskEntity taskEntity) {
        return taskMapper.insert(taskEntity);
    }

    /**
     * 修改任务查询
     *
     * @param taskEntity 任务查询
     * @return 结果
     */
    @Override
    public int updateTask(TaskEntity taskEntity) {
        return taskMapper.updateById(taskEntity);
    }

    @Override
    public int deleteTaskByTaskIds(Long[] taskIds) {
        return 0;
    }

    @Override
    public int deleteTaskByTaskId(Long taskId) {
        return 0;
    }

    @Override
    public ProcResult cancelTaskByIds(Long[] taskIds, Long creator) {
        if (taskIds == null || taskIds.length == 0) {
            return ProcResult.error("cancel_task_001", "取消任务失败!任务ID不能为空!");
        }
        int successCount = 0;
        int failCount = 0;
        for (Long taskId : taskIds) {
            try {
                ProcResult procResult = taskCancelProcess.call(taskId, creator);
                if (WmsConstants.PROC_SUCCESS.equals(procResult.getCode())) {
                    successCount++;
                } else {
                    failCount++;
                }
            } catch (Exception e) {
                failCount++;
            }
        }
        // 如果全部失败则返回异常信息
        if (successCount > 0) {
            return ProcResult.success("任务取消完成!成功取消【" + successCount + "】条，失败【" + failCount + "】条");
        } else
            return ProcResult.error("cancel_task_000", "任务取消异常!失败【" + failCount + "】条");
    }
}
